﻿//
// Copyright (c) 2024 BPer
//
#include <bperrtc/BPerPlayTrackVideo.h>
#include <bperrtc/BPerPlayTrackH265.h>

#include <bperrtp/BPerRtpHeader.h>
#include <bperrtp/BPerRtpConstant.h>
#include <bperrtp/BPerRtpRawPayload.h>
#include <bperrtp/BPerRtpFUAPayload2.h>
#include <bperrtp/BPerRtpSTAPPayload.h>

#include <bperutil/sys/BPerLog.h>
#include <bperutil/sys/BPerEndian.h>
#include <bperavutil/video/BPerMeta.h>

#include <bperrtc/BPerRtcRtcp.h>
#include <bperrtc/BPerPlayTrack.h>
#include <bperrtc/BPerPlayTrackH264.h>

#if BPer_Enable_RTC_Video

int32_t bPer_playvideoTrack_on_rtp(BPerRtcContext *context,
		BPerPlayTrackSession *videotrack, BPerRtpPacket *src) {
	if(context==NULL||videotrack==NULL) return 1;

	int32_t err = BPer_Ok;
	if (videotrack->h264Track)
		bPer_playtrackH264_on_rtp(context, (BPerPlayTrackH264*)videotrack->h264Track, src);
	if (videotrack->h265Track)
		bPer_playtrackH265_on_rtp(context, (BPerPlayTrackH265*)videotrack->h265Track, src);
	return err;
}

int32_t bPer_playvideoTrack_check_send_nacks(BPerRtcContext *context,
		BPerPlayTrackSession *videotrack) {
	int32_t err = BPer_Ok;

	uint32_t timeout_nacks = 0;

	if ((err = bPer_playtrack_do_check_send_nacks(context, &videotrack->track,
			&timeout_nacks)) != BPer_Ok) {
		return bPer_error_wrap(err, "video");
	}

	if (timeout_nacks == 0) {
		return err;
	}

	return err;
}
void bPer_playvideoTrack_setRequestKeyframeState(BPerPlayTrackSession* track,int32_t state){
	if (track->h264Track)		((BPerPlayTrackH264*)track->h264Track)->hasRequestKeyframe = state;
	if (track->h265Track)       ((BPerPlayTrackH265*)track->h265Track)->hasRequestKeyframe = state;

}

int32_t bPer_playvideoTrack_send_rtcp_rr(BPerRtcContext *context,BPerPlayTrack *track){
	return bPer_playtrack_send_rtcp_rr(context,BPerFalse,track);
}

void bPer_create_playvideoTrack(BPerRtcContext *context,BPerPlayTrackVideo *track) {
	if (track == NULL)		return;
	BPerPlayTrackSession *videotrack=&track->session;
	bPer_create_playTrack(context, &videotrack->track,BPerFalse);

	videotrack->h264Track = NULL;
	videotrack->h265Track = NULL;

	if (context->videoCodec == BPer_VED_H264) {
		BPerPlayTrackH264* h264Track=(BPerPlayTrackH264*) bPer_calloc(1,sizeof(BPerPlayTrackH264));
		videotrack->h264Track = h264Track;
		bPer_create_playTrackH264(context, h264Track);
	}
	if (context->videoCodec == BPer_VED_H265) {
		BPerPlayTrackH265* h265Track= (BPerPlayTrackH265*) bPer_calloc(1,
				sizeof(BPerPlayTrackH265));
		videotrack->h265Track =h265Track;
		bPer_create_playTrackH265(context, h265Track);
	}

	track->on_rtp=bPer_playvideoTrack_on_rtp;
	track->check_nacks=bPer_playvideoTrack_check_send_nacks;
	track->setRequestKeyframeState=bPer_playvideoTrack_setRequestKeyframeState;
	track->has_ssrc=bPer_playtrack_has_ssrc;
	track->update_rtt=bPer_playtrack_update_rtt;
	track->update_send_report_time=bPer_playtrack_update_send_report_time;
	track->send_rtcp_rr=bPer_playvideoTrack_send_rtcp_rr;
	track->send_rtcp_xr_rrtr=bPer_playtrack_send_rtcp_xr_rrtr;
	track->on_nack=bPer_playtrack_on_nack;

}

void bPer_destroy_playvideoTrack(BPerPlayTrackVideo *track) {
	if(track==NULL) return ;
	BPerPlayTrackSession *videotrack=&track->session;
	bPer_destroy_playTrack(&videotrack->track);
	bPer_free(videotrack->h264Track);
	bPer_free(videotrack->h265Track);
}
#endif

